package com.lmpop.trade.ti_custlevel;

import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import com.lmpop.frame.QueryList;
import com.lmpop.frame.SysMap;
import com.lmpop.frame.TradeInterf;
import com.lmpop.frame.dao.DbmInterf;
import com.lmpop.frame.exp.ApplicationException;

public class Ti_custlevelInfo
  implements TradeInterf
{
  QueryList queryList;

  public Ti_custlevelInfo()
  {
    this.queryList = new QueryList();
  }

  public SysMap insert(DbmInterf dbmMgr, SysMap map)
  {
    Ti_custlevel ti_custlevel = new Ti_custlevel();

    ti_custlevel.setCust_id(map.getString("cust_id"));
    ti_custlevel.setCust_class(map.getString("cust_class"));
    String cust_start_date = "2000-1-1 00:00:00";
    if (!map.getString("cust_start_date").equals("")) {
      cust_start_date = map.getString("cust_start_date");
    }
    String cust_end_date = "2050-1-1 00:00:00";
    if (!map.getString("cust_end_date").equals("")) {
      cust_end_date = map.getString("cust_end_date");
    }
    ti_custlevel.setCust_start_date(cust_start_date);
    ti_custlevel.setCust_end_date(cust_end_date);
    ti_custlevel.setUser_id(map.getString("user_id"));

    String result_code = "0";
    try {
      insert(dbmMgr, ti_custlevel);
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void insert(DbmInterf dbmMgr, Ti_custlevel ti_custlevel)
    throws ApplicationException
  {
    dbmMgr.update("insertTi_custlevel", ti_custlevel);
  }

  public SysMap delete(DbmInterf dbmMgr, SysMap map)
  {
    String pkid_string = map.getString("pkid");
    String result_code = "0";
    try {
      if (!pkid_string.equals("")) {
        String[] pkStr = pkid_string.split("\\|");
        for (int i = 0; i < pkStr.length; i++) {
          if (pkStr[i].equals("")) continue; delete(dbmMgr, pkStr[i]);
        }
      }
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void delete(DbmInterf dbmMgr, String pkid)
    throws ApplicationException
  {
	  //delete from ti_custlevel where cust_id=#value#
    dbmMgr.update("deleteTi_custlevel", pkid);
  }

  public SysMap update(DbmInterf dbmMgr, SysMap map)
  {
    Ti_custlevel ti_custlevel = new Ti_custlevel();
    ti_custlevel.setCust_id(map.getString("cust_id"));
    ti_custlevel.setCust_class(map.getString("cust_class"));

    ti_custlevel.setCust_start_date(map.getString("cust_start_date"));
    ti_custlevel.setCust_end_date(map.getString("cust_end_date"));
    ti_custlevel.setUser_id(map.getString("pub_user_id"));
    List checkList = this.queryList.getList("checkTi_custlevelByCustID", map.getString("cust_id"));

    String result_code = "0";
    try {
      if (checkList.size() > 0) {
        if (("".equals(ti_custlevel.getCust_start_date())) || (ti_custlevel.getCust_start_date() == null))
        {
          updateCustClass(dbmMgr, map);
        }
        else
        {
          update(dbmMgr, ti_custlevel);
        }
      }
      else insert(dbmMgr, ti_custlevel);
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_custlevel ti_custlevel)
    throws ApplicationException
  {
    dbmMgr.update("updateTi_custlevel", ti_custlevel);
  }
  public SysMap updatecustclass_error(DbmInterf dbmMgr, SysMap map) {
    Ti_custlevel ti_custlevel = new Ti_custlevel();
    ti_custlevel.setCust_id(map.getString("cust_id"));
    ti_custlevel.setCust_class(map.getString("cust_class"));
    ti_custlevel.setUser_id(map.getString("pub_user_id"));
    List checkList = this.queryList.getList("checkTi_custlevelByCustID", map.getString("cust_id"));
    String result_code = "0";
    try {
      if (checkList.size() > 0)
        updatecustclass(dbmMgr, ti_custlevel);
      else
        insert(dbmMgr, ti_custlevel);
    }
    catch (ApplicationException e)
    {
      e.printStackTrace();
      result_code = "1";
    }
    map.setString("result_code", result_code);
    return map;
  }

  public void updatecustclass(DbmInterf dbmMgr, Ti_custlevel ti_custlevel) throws ApplicationException {
    dbmMgr.update("updateTi_custlevelcust_class", ti_custlevel);
  }

  public List getListByPage(Object obj, int start, int limit)
  {
    Ti_custlevel ti_custlevel = (Ti_custlevel)obj;
    if (start == 0)
      start = 0;
    else {
      start = (start - 1) * limit;
    }
    ti_custlevel.setStart(Integer.valueOf(start));
    ti_custlevel.setLimit(Integer.valueOf(limit));
    return this.queryList.getList("getListByTi_custlevelPage", ti_custlevel);
  }

  public int getCountByObj(Object obj)
  {
    Ti_custlevel ti_custlevel = (Ti_custlevel)obj;
    List list = this.queryList.getList("getCountByObjTi_custlevel", obj);
    int count = 0;
    if ((list != null) && (list.size() > 0)) {
      Hashtable newsMap = (Hashtable)list.get(0);
      if ((newsMap.get("count") != null) && (!newsMap.get("count").toString().equals(""))) {
        count = Integer.parseInt(newsMap.get("count").toString());
      }
    }
    return count;
  }

  public List getListByPk(String pk)
  {
    return this.queryList.getList("getTi_custlevelByPk", pk);
  }

  public SysMap updateCustClass(DbmInterf dbmMgr, SysMap map) {
    Ti_custlevel ti_custlevel = new Ti_custlevel();
    ti_custlevel.setCust_id(map.getString("cust_id"));
    ti_custlevel.setCust_class(map.getString("cust_class"));
    ti_custlevel.setCust_start_date(new Date().toLocaleString());
    ti_custlevel.setCust_end_date(map.getString("cust_end_date"));
    ti_custlevel.setUser_id(map.getString("user_id"));
    String result_code = "0";
    try {
      update(dbmMgr, ti_custlevel, map.getString("oldcust_class"), map.getString("lhswPwd"));
    } catch (ApplicationException e) {
      e.printStackTrace();
      result_code = "1";
    }

    map.setString("result_code", result_code);
    return map;
  }

  public void update(DbmInterf dbmMgr, Ti_custlevel ti_custlevel, String oldcust_class, String lhswPwd) throws ApplicationException {
    List checkList = this.queryList.getList("checkTi_custlevelByCustID", ti_custlevel.getCust_id());
    if (checkList.size() > 0) {
      dbmMgr.update("updateTi_custlevel", ti_custlevel);
      Map map = new Hashtable();
      map.put("cust_id", ti_custlevel.getCust_id());
      map.put("user_class", ti_custlevel.getCust_class());
      dbmMgr.update("upateMemberUserClass", map);
      if ("9".equals(ti_custlevel.getCust_class())) {
        map.put("by1", lhswPwd);
        dbmMgr.update("upateMemberBy1", map);
      }
      if (oldcust_class.equals("9")) {
        map.put("by1", "");
        dbmMgr.update("upateMemberBy1", map);
      }
    }
    else {
      insert(dbmMgr, ti_custlevel);
    }
  }

  public String checkCustLevel(String cust_id)
  {
    String levelvalid = "0";
    List levellist = this.queryList.getList("checkcustleveldate", cust_id);
    if ((levellist != null) && (levellist.size() > 0))
    {
      levelvalid = "1";
    }
    return levelvalid;
  }
}